﻿namespace PugLib.Math
{
	using System;
	using System.Diagnostics;
	using System.Security.Cryptography;

	public static class PugRandom
	{
		public static Int32 GetRandomInRangeRng(Int32 min, Int32 max)
		{
			if (min == max)
			{
				return min;
			}

			if (min > max)
			{
				Int32 tmp = max;
				max = min;
				min = tmp;
			}

			using (RNGCryptoServiceProvider gen = new RNGCryptoServiceProvider())
			{
				Byte[] randomNumber = new Byte[1];
				Int32 rand;
				do
				{
					// Fill the array with a random value.
					gen.GetBytes(randomNumber);
					// Convert the Byte to an integer value to make the modulus operation easier.
					rand = Convert.ToInt32(randomNumber[0]) + min;
					rand = rand % max;
				} while (rand < min || rand > max);
				Debug.Assert(rand >= min && rand <= max);
				return rand;
			}
		}

		public static Int32 GetRandomInRange(Int32 min, Int32 max)
		{
			Random rnd = new Random();
			rnd.NextDouble();
			return rnd.Next(min, max);
		}
	}
}